#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: weight
    Description:
        Author: lhw
        Date: 2020/9/21
-----------------------------------
"""
import requests
from rest_framework.views import APIView
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from Baby import models
from Baby.serializers import weight
from Common.logger import app_log
from Common.response import resp


class WeightView(APIView):
    # authentication_classes = [JwtAuthorizationAuthentication, ]

    def get(self, request, *args, **kwargs):
        try:
            weight_query = models.Weight.objects.all()
            weight_data = weight.WeightModelSerializer(weight_query, many=True).data
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": weight_data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def post(self, request, *args, **kwargs):
        try:
            request_data = request.data
            weight_ser = weight.WeightModelSerializer(data=request_data)
            weight_ser.is_valid(raise_exception=True)
            weight_obj = weight_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": weight.WeightModelSerializer(weight_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def put(self, request, *args, **kwargs):
        try:
            request_data = request.data
            weight_id = request_data["id"]
            old_weight_obj = models.Weight.objects.filter(id=weight_id).first()
            weight_ser = weight.WeightModelSerializer(instance=old_weight_obj,
                                                      data=request_data, partial=True)
            weight_ser.is_valid(raise_exception=True)
            weight_obj = weight_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": weight.WeightModelSerializer(weight_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def delete(self, request, *args, **kwargs):
        try:
            weight_id = request.GET.get("id")
            weight_data = models.Weight.objects.filter(id=weight_id).first()
            if weight_data:
                models.Weight.objects.filter(id=weight_id).delete()
                return Response({
                    "error_code": 0,
                    "message": "Success"
                })
            else:
                return resp(2005, {})
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)